#include "../includes/TaskManager.h"
#include <mutex>
#include <cstring>
#include <algorithm> 


TaskManager* TaskManager::instance_ = nullptr;
std::queue<std::string> TaskManager::tasks;
std::queue<std::string> TaskManager::responses;
std::mutex TaskManager::task_mtx;
std::mutex TaskManager::resp_mtx;

TaskManager& TaskManager::getInstance(){
    if(instance_ == nullptr){
        instance_ = new TaskManager();
    }
    return *instance_;
}

TaskManager::TaskManager(){
    
}

void TaskManager::add_task(const std::string &task_str){
    std::lock_guard<std::mutex> lock(task_mtx);
    tasks.push(task_str);
}

void TaskManager::get_task(std::string &task_str){
    std::lock_guard<std::mutex> lock(task_mtx);
    task_str = tasks.front();
    tasks.pop();
}


bool TaskManager::task_empty(){
    std::lock_guard<std::mutex> lock(task_mtx);
    return tasks.empty();
}

int TaskManager::task_type(const std::string &task_str){
    return static_cast<int>(task_str[4]); // 第5个字节是任务类型
}



void TaskManager::add_resp(const std::string &resp_str){
    std::lock_guard<std::mutex> lock(resp_mtx);
    responses.push(resp_str);
}

void TaskManager::get_resp(std::string &resp_str){
    std::lock_guard<std::mutex> lock(resp_mtx);
    resp_str = responses.front();
    responses.pop();
}


bool TaskManager::resp_empty(){
    std::lock_guard<std::mutex> lock(resp_mtx);
    return responses.empty();
}

int TaskManager::resp_type(const std::string &resp_str){
    return static_cast<int>(resp_str[4]); // 第5个字节是任务类型
}

